package com.itheima.controller;

import com.itheima.common.R;
import com.itheima.entity.dto.dish.DishAddDto;
import com.itheima.entity.dto.dish.DishUpDto;
import com.itheima.entity.vo.dish.DishByIdVo;
import com.itheima.entity.vo.dish.DishListByCategoryVo;
import com.itheima.entity.vo.dish.DishPageVo;
import com.itheima.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @author huangzelnag
 * @create 2022-05-09
 */

@RestController
@RequestMapping("/dish")
public class DishController {


    @Autowired
    DishService dishService;

    /**
     * 通过分页获取列表
     *
     * @param name
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("page")
    public R getDishPage(@RequestParam(required = false) String name,
                         @RequestParam(required = false) Integer page,
                         @RequestParam(required = false) Integer pageSize
    ) {
        DishPageVo dishPage = dishService.getDishPage(name, page, pageSize);

        if (Objects.isNull(dishPage)) {
            return R.error("获取到的对象为空");
        }
        return R.success(dishPage);

    }


    /**
     * 添加菜品
     *
     * @param dishAddDto
     * @return
     */
    @PostMapping
    public R addDish(@RequestBody DishAddDto dishAddDto) {

        Boolean flag = dishService.addDish(dishAddDto);
        if (flag) {
            return R.success("新增菜品成功");
        } else {
            return R.error("错误");
        }

    }


    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public R delDishById(@RequestParam String ids) {
        String[] id = ids.split(",");

        Boolean aBoolean = dishService.delDishById(id);

        if (aBoolean) {
            return R.success("添加成功");
        }
        return R.error("删除失败");
    }

    @GetMapping("/{id}")
    public R getDishById(@PathVariable String id) {

        DishByIdVo dishById = dishService.getDishById(id);

        if (Objects.isNull(dishById)) {
            return R.error("查不到这个id信息");
        }

        return R.success(dishById);

    }


    /**
     * 菜品启用或者禁用
     *
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R upDishStatus(@PathVariable Integer status,
                          @RequestParam String ids) {
        String[] idArray = ids.split(",");

        Boolean flag = dishService.upDishStatus(status,idArray);

        if(flag){
            return R.success("操作成功");
        }
        return R.error("不能够操作");
    }



    @PutMapping
    public R upDishInfo(@RequestBody DishUpDto dishUpDto){

        Boolean aBoolean = dishService.upDishInfo(dishUpDto);

        if(aBoolean){
            return R.success("修改菜品成功");
        }
        return R.error("修改失败");

    }


    /**
     * 根据分类id查询
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public R  getDishListByCategoryId(@RequestParam String categoryId)  {
        List<DishListByCategoryVo> dishListByCategoryId = dishService.getDishListByCategoryId(categoryId);

//        if(CollUtil.isEmpty(dishListByCategoryId)){
//            List<DishListByCategoryVo> list= new ArrayList<>();
//            return R.success(list);
//        }

        return R.success(dishListByCategoryId);
    }





}
